<template>
  <div class="app-container">

    <el-table :data="orderList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="oid" width="80">
        <template slot-scope="scope">
          {{ scope.row.oid }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="房屋id" width="180">
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content">
              地区名:{{getZone(scope.row.information.house.zoneId)}}
              </br>
              小区名:{{scope.row.information.house.village}}
            </div>
            <el-link>{{ scope.row.hid }}</el-link>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="买家id" width="100">
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content">
              买家姓名:{{scope.row.information.buyer.name}}
              </br>
              买家电话:{{scope.row.information.buyer.phone}}
            </div>
            <el-link>{{ scope.row.bid }}</el-link>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="卖家id" width="220">
        <div v-if="scope.row.sid == undefined" slot-scope="scope">
          <template>
            此房源销售经理代理
          </template>
        </div>
        <div v-else slot-scope="scope">
          <template>
            <el-tooltip placement="top">
              <div slot="content">
                卖家姓名:{{scope.row.information.saler.name}}
                </br>
                卖家电话:{{scope.row.information.saler.phone}}
              </div>
              <el-link>{{ scope.row.sid }}</el-link>
            </el-tooltip>
          </template>
        </div>
      </el-table-column>
      <el-table-column align="center" label="经理id" width="120" v-if="$store.getters.roles[0] !== 'Manager'">
        <div v-if="scope.row.aid == undefined" slot-scope="scope">
          <template>
            此房源自行交易
          </template>
        </div>
        <div v-else slot-scope="scope">
          <template>
            <el-tooltip placement="top">
              <div slot="content">
                经理姓名:{{scope.row.information.admin.name}}
                </br>
                经理电话:{{scope.row.information.admin.phone}}
              </div>
              <el-link>{{ scope.row.aid }}</el-link>
            </el-tooltip>
          </template>
        </div>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="120">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="交易程度" width="120">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.step" size="small">
              <el-option v-for="item in steps" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-button class="cancel-btn" size="small" icon="el-icon-refresh" type="warning" @click="cancelEdit(row)">
              取消
            </el-button>
          </template>
          <span v-else>{{ getStep(row.step) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.edit" type="success" size="small" icon="el-icon-circle-check-outline"
            @click="confirmEdit(scope.row)">
            确定
          </el-button>
          <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="scope.row.edit=!scope.row.edit">
            修改订单
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页查询 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="dataTotal"
      v-show="dataTotal>0">
    </el-pagination>
  </div>
</template>

<script>
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import {
    getOrderByRolePage,
    editOrderStep
  } from '@/api/order'

  import {
    getData
  } from '@/api/data'

  import {
    deepClone
  } from '@/utils'

  export default {
    data() {
      return {
        orderList: [],
        zones: [],
        steps: [],
        // 分页
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 15, 20, 100],
        dataTotal: 0
      }
    },
    computed: {
      getZone() {
        if (this.zones) {
          return function(zoneId) {
            return this.zones[zoneId - 1].label;
          }
        }
      },
      getStep() {
        if (this.steps) {
          return function(step) {
            return this.steps[step - 1].label;
          }
        }
      },
    },
    created() {
      this.getOrder()
      this.getZones()
      this.getSteps()
    },
    methods: {
      async getZones() {
        const {
          data
        } = await getData("zone_id")
        for (var i = 0; i < data.length; i++) {
          let obj = {
            value: data[i].valueEn,
            label: data[i].value
          }
          this.zones.push(obj)
        }
      },
      async getSteps() {
        const {
          data
        } = await getData("step")
        for (var i = 0; i < data.length; i++) {
          let obj = {
            value: data[i].valueEn,
            label: data[i].value
          }
          this.steps.push(obj)
        }
      },
      // 获取数据库员工信息
      async getOrder() {
        const {
          data
        } = await getOrderByRolePage(this.currentPage, this.pageSize)
        const items = data.data
        this.orderList = items.map(v => {
          this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
          v.step = String(v.step)
          v.originalStep = v.step //  will be used when user click the cancel botton
          return v
        })
        this.dataTotal = data.total
        console.log(this.orderList)
      },
      // 添加员工
      handleAddPersonnel() {
        // this.dialogType = 'new'
        // this.dialogVisible = true
        // this.personnel = {}
      },
      // 修改员工
      handleEdit(scope) {
        // this.personnel = deepClone(scope.row)
        // this.dialogType = 'edit'
        // this.dialogVisible = true
      },
      // 分页功能
      handleSizeChange(val) {
        this.pageSize = val
        this.getOrder()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.getOrder()
      },
      // 删除
      handleDelete({
        $index,
        row
      }) {
        this.$confirm('确定删除?', 'Warning', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(async () => {
            const res = await deleteOrder(row.id)
            if (res.code === 1) {
              this.orderList.splice($index, 1)
              this.$message({
                type: 'success',
                message: res.msg
              })
            }
          })
          .catch(err => {
            console.error(err)
          })
      },
      // 修改订单状态
      cancelEdit(row) {
        row.step = row.originalStep
        row.edit = false
        this.$message({
          message: '订单状态未修改',
          type: 'warning'
        })
      },
      confirmEdit(row) {
        row.edit = false
        row.originalStep = row.step
        editOrderStep(row.oid, row.step).then(res => {
          if (res.code === 1) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
